<template>
	<view class="resources-context">
		<u-navbar :is-back="true" title="我的二手车">
		</u-navbar>
		<view>
			<u-tabs-swiper ref="uTabs" :list="tabsList" :current="tabsCurrent" @change="tabsChange"
				:is-scroll="false"></u-tabs-swiper>
		</view>
		<swiper style="height: 84vh;" :current="swiperCurrent" @transition="transition"
			@animationfinish="animationfinish">
			<swiper-item class="swiper-item" v-for="(i,tabIndex) in tabsList" :key="tabIndex">
				<scroll-view scroll-y style="height: 84vh;width: 100%;" @scrolltolower="onreachBottom" v-if="queryList.length != 0">
					<view class="query-list">
						<view class="list-item" v-for="item in queryList" :key="item.id" @click="gotoNext(item.id)">
							<view class="not-agree" v-if="tabsCurrent == 1">
								<view class="not-text">未通过原因：内容不符合，虚假信息</view>
								<view class="goUpdate" @click="gotoUpdate(item.id)">去修改</view>
							</view>
							<view style="display: flex;">
								<view class="item-image">
									<image :src="'https://afyc.chitong.cn/uploads/' + item.img" mode=""></image>
								</view>
								<view class="item-detail">
									<view class="item-name">{{item.title}}</view>
									<view class="item-buttom">
										<view class="buttom-left">
											<view class="item-address">{{item.address}}</view>
											<view class="item-price">预估售价：<text>{{item.price}}万元</text></view>
										</view>
										<view class="item-cancat">
											<!-- <image src="../../static/image/call.png" mode=""></image>
											<view>联系车主</view> -->
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
					<u-divider bg-color="rgba(0,0,0,0)" v-if="hasMore">没有更多了</u-divider>
				</scroll-view>
				<view class="not-data" v-else>
					<image src="../../static/image/notData.png" mode=""></image>
				</view>
			</swiper-item>

		</swiper>
	</view>
</template>

<script>
	import {
		getMyCarList
	} from '@/api/index.js'
	export default {
		data() {
			return {
				// 没有更多
				hasMore: true,
				tabsList: [],
				tabsCurrent: 0, // tabs组件的current值，表示当前活动的tab选项
				swiperCurrent: 0, // swiper组件的current值，表示当前那个swiper-item是活动的
				tabsList: [{
					name: '审核中'
				}, {
					name: '未通过'
				}, {
					name: '展示中'
				}, {
					name: '已下架',
				}],
				// 列表最后一夜
				last_page: 0,
				// 查询表单
				queryForm: {
					page: 1,
					limit: 10,
					is_sale: null,
					is_open: null
				},
				queryList: []
			}
		},
		onShow() {
			this.reset()
			this.getMyCarList()
		},
		methods: {
			// 获取我的房源列表
			// 审核中is_sale=0，is_open=0，未通过is_sale=0，is_open=2
			// 展示中is_sale=1，is_open=1，已下架is_sale=0，is_open=1
			async getMyCarList() {
				await this.handleQuery()
				let classArr = ['color1', 'color2', 'color3', 'color4']
				await getMyCarList(this.queryForm).then(res => {
					this.last_page = res.last_page
					this.queryList = [...this.queryList, ...res.data]
					this.queryList.forEach(item => {
						if (item.biaoqian !== null) {
							let index = 0
							item.biaoqian = item.biaoqian.map((ele) => {
								const className = classArr[index]
								index++;
								(index == 4) && (index == 0);
								return {
									className,
									name: ele
								}
							})
						}
					})
				})
			},
			// 处理查询参数
			handleQuery() {
				if (this.tabsCurrent == 0) {
					this.queryForm.is_sale = 0
					this.queryForm.is_open = 0
				}
				if (this.tabsCurrent == 1) {
					this.queryForm.is_sale = 0
					this.queryForm.is_open = 2
				}
				if (this.tabsCurrent == 2) {
					this.queryForm.is_sale = 1
					this.queryForm.is_open = 1
				}
				if (this.tabsCurrent == 3) {
					this.queryForm.is_sale = 0
					this.queryForm.is_open = 1
				}
			},
			gotoNext(id) {
				uni.navigateTo({
					url: '/pages/usedCarDetail/index?carId=' + id + '&pageStatus=1&tabsCurrent=' + this.tabsCurrent
				})
			},
			gotoUpdate(id){
				uni.navigateTo({
					url:'/pages/releaseCar/index?carId=' + id
				})
			},
			// tabs通知swiper切换
			tabsChange(index) {
				this.swiperCurrent = index;
			},
			// swiper-item左右移动，通知tabs的滑块跟随移动
			transition(e) {
				let dx = e.detail.dx;
				this.$refs.uTabs.setDx(dx);
			},
			// 由于swiper的内部机制问题，快速切换swiper不会触发dx的连续变化，需要在结束时重置状态
			// swiper滑动结束，分别设置tabs和swiper的状态
			async animationfinish(e) {
				let current = e.detail.current;
				this.$refs.uTabs.setFinishCurrent(current);
				this.swiperCurrent = current;
				this.tabsCurrent = current;
				await this.reset()
				await this.getMyCarList()
			},
			// 重置查询表单，和列表
			reset() {
				this.queryForm.page = 1
				this.queryForm.limit = 10
				this.queryList = []
			},
			// scroll-view到底部加载更多
			onreachBottom() {
				if (this.queryForm.page != this.last_page) {
					this.queryForm.page++
					this.getMyCarList()
					this.hasMore = false
					return
				} else {
					this.hasMore = true
				}
			}
		}
	}
</script>

<style lang="scss">
	.not-data {
		width: 100%;
		height: 500rpx;
	
		>image {
			width: 100%;
			height: 100%;
		}
	}
	.slot-wrap {
		display: flex;
		// padding: 0 20rpx;
		justify-content: space-between;
		width: 100%;
	}

	.query-list {
		padding: 20rpx;

		.list-item {
			background-color: #fff;
			border-radius: 8rpx;
			padding: 20rpx;
			// display: flex;
			margin-top: 20rpx;

			.not-agree {
				display: flex;
				padding: 15rpx 10rpx;
				margin-bottom: 20rpx;
				background-color: #FEF4F4;
				border-radius: 8rpx;
				font-size: 24rpx;
				justify-content: space-between;
				align-items: center;

				.not-text {
					color: #FF5151;
				}

				.goUpdate {
					color: #fff;
					padding: 10rpx 15rpx;
					background-color: #FD8A19;
					border-radius: 25rpx;
				}
			}

			.item-image {
				width: 230rpx;
				height: 172rpx;
				margin-right: 10rpx;

				>image {
					border-radius: 15rpx;
					width: 100%;
					height: 100%;
				}
			}

			.item-detail {
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.item-name {
					color: #333333;
					font-size: 26rpx;
				}

				.item-buttom {
					display: flex;
					justify-content: space-between;

					.buttom-left {
						font-size: 22rpx;
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						color: #999999;
					}

					.item-price {
						font-size: 22rpx;
						color: #333333;

						>text {
							font-size: 28rpx;
							color: #FE3939;
						}
					}


					.item-cancat {
						margin-top: 10rpx;
						color: #666666;
						font-size: 18rpx;
						text-align: center;

						>image {
							width: 45rpx;
							height: 45rpx;
						}
					}
				}
			}

		}
	}
</style>